package com.cnhis.drgs.util;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ReflectUtil {

    /**
     * 获取父子类中所有Field(包括公共、保护、私有方法)
     *
     * @param clazz
     * @return
     */
    public static List<Field> getClassAllField(Class<?> clazz) {
        List<Field> fields = new ArrayList<>();
        // 当前类本身Field
        Field[] this_fields = clazz.getDeclaredFields();
        fields.addAll(Arrays.asList(this_fields));
        // 当前类的父类
        Class<?> super_clazz = clazz.getSuperclass();
        if (super_clazz != Object.class) {
            // 父类还有父类
            fields.addAll(getClassAllField(super_clazz));
        }
        return fields;
    }

    /**
     * 获取父子类中所有Method(包括公共、保护、私有方法)
     *
     * @param clazz
     * @return
     */
    public static List<Method> getClassAllMethod(Class<?> clazz) {
        List<Method> methods = new ArrayList<>();
        // 当前类本身Field
        Method[] this_methods = clazz.getDeclaredMethods();
        methods.addAll(Arrays.asList(this_methods));
        // 当前类的父类
        Class<?> super_clazz = clazz.getSuperclass();
        if (super_clazz != Object.class) {
            // 父类还有父类
            methods.addAll(getClassAllMethod(super_clazz));
        }
        return methods;
    }

    /**
     * 对象转map
     *
     * @param object
     * @return
     */
    public static Map<String, Object> object2Map(Object object) {
        try {

            // apache commons
            //Map<String, String> map = BeanUtils.describe(object);
            //map.remove("class");

            // apache commons
            //Map<Object,Object> map= new org.apache.commons.beanutils.BeanMap(object);

            //spring
            Map<String, Object> map = BeanMap.create(object);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 设置对象属性值
     *
     * @param bean
     * @param name
     * @param value
     */
    public static void setObjectProperty(Object bean, String name, Object value) {
        try {
            BeanUtils.setProperty(bean, name, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取对象属性值
     *
     * @param bean
     * @param name
     * @return
     */
    public static Object getObjectProperty(Object bean, String name) {
        try {

            //返回值是string，不太好
            //return BeanUtils.getProperty(bean, name);

            return PropertyUtils.getProperty(bean, name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 拷贝对象
     *
     * @param dest
     * @param orig
     */
    public static void copyObject(Object dest, Object orig) {
        try {
            BeanUtils.copyProperties(dest, orig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
